package com.example.cheapshark.mobile;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class GameDeal {
	private String internalName;
	private String title;
	private String dealID;
	private int storeID;
	private int gameID;
	private BigDecimal salePrice;
	private BigDecimal normalPrice;
	private int score;
	private Date releaseDate;
	private Date lastChange;
	private double dealRating;
	private Bitmap thumb;
	private final SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM d, yyyy");
	
	public GameDeal()
	{
	
	}
	public GameDeal(JSONObject json) throws ClientProtocolException, IOException
	{
		try {
			this.setInternalName(json.getString("internalName"));
			this.setTitle(json.getString("title"));
			this.setDealID(json.getString("dealID"));
			this.setStoreID(json.getInt("storeID"));
			this.setGameID(json.getInt("gameID"));
			this.setSalePrice(new BigDecimal(json.getString("salePrice")));
			this.setNormalPrice(new BigDecimal(json.getString("normalPrice")));
			this.setScore(json.getInt("score"));
			this.setReleaseDate(json.getString("releaseDate"));
			this.setLastChange(json.getString("lastChange"));
			this.setDealRating(json.getDouble("dealRating"));
			this.setThumb(json.getString("thumb"));	
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static GameDeal[] ConstructArray(JSONArray array) throws ClientProtocolException, IOException, JSONException
	{
		GameDeal[] gameDeals = new GameDeal[array.length()];
		for(int i = 0; i < array.length(); i++)
		{
			gameDeals[i] = new GameDeal(array.getJSONObject(i)); 
		}
		return gameDeals;
	}
	public String getInternalName() {
		return internalName;
	}
	public void setInternalName(String internalName) {
		this.internalName = internalName;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getDealID() {
		return dealID;
	}
	public void setDealID(String dealID) {
		this.dealID = dealID;
	}
	public int getStoreID() {
		return storeID;
	}
	public void setStoreID(int storeID) {
		this.storeID = storeID;
	}
	public int getGameID() {
		return gameID;
	}
	public void setGameID(int gameID) {
		this.gameID = gameID;
	}
	public BigDecimal getSalePrice() {
		return salePrice;
	}
	public void setSalePrice(BigDecimal salePrice) {
		this.salePrice = salePrice;
	}
	public BigDecimal getNormalPrice() {
		return normalPrice;
	}
	public void setNormalPrice(BigDecimal normalPrice) {
		this.normalPrice = normalPrice;
	}
	public int getScore() {
		return score;
	}
	public void setScore(int score) {
		this.score = score;
	}
	public Date getReleaseDate() {
		return releaseDate;
	}
	public void setReleaseDate(String date) {
		try {
			this.releaseDate = dateFormat.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			this.releaseDate = null;
		}
	}
	public Date getLastChange() {
		return lastChange;
	}
	public void setLastChange(String date) {
		try {
			this.lastChange = dateFormat.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			this.lastChange = null;
		}
	}
	public double getDealRating() {
		return dealRating;
	}
	public void setDealRating(double dealRating) {
		this.dealRating = dealRating;
	}
	public Bitmap getThumb() {
		return thumb;
	}
	public void setThumb(String uri) throws ClientProtocolException, IOException {
		byte[] data = HTTPRequester.ByteRequester(uri);
		this.thumb = BitmapFactory.decodeByteArray(data, 0, data.length);
	}
}
